#include "/node_modules/lygia/generative/cnoise.glsl";
#include "/node_modules/lygia/math/const.glsl";

uniform float iTime;
uniform vec3 iResolution;
uniform vec4 iMouse;
uniform float uDistort;
uniform float uFrequency;

varying float vNoise;
varying vec3 vNormal;
varying vec3 vWorldPosition;

varying vec2 vUv;

vec3 distort(vec3 p){
    float offset = cnoise(p / uFrequency + iTime * .5);

    float t = (p.y + offset) * PI * 12.;
    float noise= (sin(t) * p.x + cos(t) * p.z) * 2.;
    noise *= uDistort;

    vNoise = noise;
    p += noise * normal * .01;
    return p;
}

// 要放在这里，因为会用到 distort 函数
#include "../common/fixNormal.glsl";

void main(){
    vec3 p = position;
    vec3 dp = distort(p);
    gl_Position = projectionMatrix * modelViewMatrix * vec4(dp, 1.);
    
    vUv=uv;

    vec3 fNormal = fixNormal(p, dp, normal, RADIUS/SEGMENTS);
    vNormal = (modelMatrix * vec4(fNormal, 0.)).xyz;

    vWorldPosition = vec3(modelMatrix*vec4(dp,1.));
}
